package com.mia.props.common;

import com.mia.craftstudio.CSModel;
import com.mia.craftstudio.minecraft.CraftStudioModelWrapper;
import com.mia.props.DecoModelMetadata;
import com.mia.props.Props;
import com.mia.props.TabProps;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.client.renderer.texture.IIconRegister;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.ChatComponentText;
import net.minecraft.util.EnumChatFormatting;
import net.minecraft.util.IChatComponent;
import net.minecraft.util.MathHelper;
import net.minecraft.world.World;

public class ItemProps extends Item {
   public ItemProps() {
      this.setMaxDamage(0);
      this.setHasSubtypes(true);
   }

   public static boolean canPlace(CraftStudioModelWrapper model, World world, int x, int y, int z, EntityPlayer player, int orient) {
      int[] extendBlock = model.getExtendPlacementBlock(orient);

      for(int lx = extendBlock[0]; lx <= extendBlock[3]; ++lx) {
         for(int ly = model.getMetadata().csmodel.getRootNode() != null && model.getMetadata().csmodel.getRootNode().hasAttribute(CSModel.ModelNode.Attrb.IGNOREBELOWYPLANE) ? 0 : extendBlock[1]; ly <= extendBlock[4]; ++ly) {
            for(int lz = extendBlock[2]; lz <= extendBlock[5]; ++lz) {
               Block targetBlock = world.getBlock(lx + x, ly + y, lz + z);
               if (targetBlock.getMaterial() != Material.air && targetBlock != Blocks.tallgrass && targetBlock != Blocks.snow_layer && (!player.isSneaking() || targetBlock != Props.blockFake) && (!player.isSneaking() || targetBlock != Props.blockProps)) {
                  return false;
               }
            }
         }
      }

      return true;
   }

   public boolean onItemUse(ItemStack stack, EntityPlayer player, World world, int x, int y, int z, int par7, float par8, float par9, float par10) {
      if (stack.stackSize == 0) {
         return false;
      } else {
         Block target = world.getBlock(x, y, z);
         if (target != Blocks.tallgrass && target != Blocks.snow_layer) {
            if (par7 == 0) {
               --y;
            } else if (par7 == 1) {
               ++y;
            } else if (par7 == 2) {
               --z;
            } else if (par7 == 3) {
               ++z;
            } else if (par7 == 4) {
               --x;
            } else if (par7 == 5) {
               ++x;
            }
         }

         if (!player.canPlayerEdit(x, y, z, par7, stack)) {
            return false;
         } else {
            DecoModelMetadata data = (DecoModelMetadata)Props.modelData.get(stack.getItemDamage());
            int i1;
            if (!data.limitRotation) {
               i1 = MathHelper.floor_double((double)(player.rotationYaw * 16.0F / 360.0F) + 0.5D) & 15;
            } else {
               int direction = MathHelper.floor_double((double)(player.rotationYaw * 4.0F / 360.0F) + 0.5D) & 3;
               i1 = direction * 4;
            }

            if (!canPlace(data.wrapper, world, x, y, z, player, i1)) {
               if (world.isRemote) {
                  IChatComponent error = new ChatComponentText("You can't place that here. Something is in the way.");
                  error.getChatStyle().setItalic(true);
                  error.getChatStyle().setColor(EnumChatFormatting.GRAY);
                  player.addChatMessage(error);
               }

               return false;
            } else if (!world.setBlock(x, y, z, Props.blockProps, 0, 3)) {
               return false;
            } else {
               TileEntity tileentity = world.getTileEntity(x, y, z);
               if (tileentity instanceof TileProps) {
                  TileProps propsTile = ((TileProps)tileentity).getNewTileEntityForMeta(stack.getItemDamage(), i1, false);
                  if (stack.hasTagCompound() && stack.getTagCompound().hasKey("savedProp")) {
                     propsTile.readFromNBT(stack.getTagCompound().getCompoundTag("savedProp"));
                     propsTile.rotation = i1;
                  }

                  world.setTileEntity(x, y, z, propsTile);
                  propsTile.init();
                  propsTile.markDirty();
                  world.markBlockForUpdate(x, y, z);
               }

               int[] extendBlock = data.wrapper.getExtendPlacementBlock(i1);
               TileProps master = (TileProps)((TileProps)world.getTileEntity(x, y, z));

               for(int lx = extendBlock[0]; lx <= extendBlock[3]; ++lx) {
                  for(int ly = data.csmodel.getRootNode() != null && data.csmodel.getRootNode().hasAttribute(CSModel.ModelNode.Attrb.IGNOREBELOWYPLANE) ? 0 : extendBlock[1]; ly <= extendBlock[4]; ++ly) {
                     for(int lz = extendBlock[2]; lz <= extendBlock[5]; ++lz) {
                        if (lx != 0 || ly != 0 || lz != 0) {
                           Block targetBlock = world.getBlock(lx + x, ly + y, lz + z);
                           TileFake fakeTile;
                           if (targetBlock == Props.blockFake) {
                              fakeTile = (TileFake)((TileFake)world.getTileEntity(lx + x, ly + y, lz + z));
                              TileProps targetMaster = fakeTile.getMaster();
                              if (targetMaster != null) {
                                 targetMaster.rmSlave(fakeTile);
                              }
                           }

                           if (targetBlock != Props.blockProps) {
                              world.setBlock(lx + x, ly + y, lz + z, Props.blockFake, 0, 3);
                              fakeTile = (TileFake)((TileFake)world.getTileEntity(lx + x, ly + y, lz + z));
                              fakeTile.setMasterTile(master);
                              master.addSlave(fakeTile);
                           }
                        }
                     }
                  }
               }

               world.playSoundEffect((double)((float)x + 0.5F), (double)((float)y + 0.5F), (double)((float)y + 0.5F), Props.blockProps.stepSound.func_150496_b(), (Props.blockProps.stepSound.getVolume() + 1.0F) / 2.0F, Props.blockProps.stepSound.getPitch() * 0.8F);
               --stack.stackSize;
               return true;
            }
         }
      }
   }

   public void getSubItems(Item item, CreativeTabs tab, List retlist) {
      ArrayList metaList = new ArrayList(Props.modelData.values());
      Collections.sort(metaList);
      Iterator var5 = metaList.iterator();

      while(true) {
         DecoModelMetadata data;
         do {
            if (!var5.hasNext()) {
               return;
            }

            data = (DecoModelMetadata)var5.next();
         } while(tab != data.tab.get() && tab != CreativeTabs.tabAllSearch && tab != null);

         retlist.add(new ItemStack(item, 1, data.decocraftModelID));
      }
   }

   public int getMetadata(int meta) {
      return meta;
   }

   public String getUnlocalizedName() {
      return "item.props.stuffed_creeper";
   }

   public String getUnlocalizedName(ItemStack stack) {
      return "item.props." + ((DecoModelMetadata)Props.modelData.get(stack.getItemDamage())).name.toLowerCase().replace(" ", "_").replace("'", "_");
   }

   @SideOnly(Side.CLIENT)
   public void registerIcons(IIconRegister registrar) {
      this.itemIcon = registrar.registerIcon("props:mobcreeper");
   }

   public CreativeTabs[] getCreativeTabs() {
      return TabProps.getAll();
   }

   @SideOnly(Side.CLIENT)
   public CreativeTabs getCreativeTab() {
      return CreativeTabs.tabAllSearch;
   }
}
